using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace DAL
{
    public class DataManagerQuery
    {
        public DataManagerQuery()
        {
            Params = new List<Param>();
            OrderBys = new List<OrderBy>();
        }

        private List<Param> _params;
        public List<Param> Params
        {
            get
            {
                return _params;
            }
            protected set
            {
                _params = value;
            }
        }
        private List<OrderBy> _orderBys;
        public List<OrderBy> OrderBys
        {
            get
            {
                return _orderBys;
            }
            protected set
            {
                _orderBys = value;
            }
        }

        public struct Param
        {
            public string ColumnName;
            public string ParameterName;
            public object Value;
            public Comparison Comparison;

            public override string ToString()
            {
                string result = "";
                string safeName = ParameterName.Replace(" ", "_");
                switch (Comparison)
                {
                    case Comparison.GreaterThan:
                        result = string.Format("[{0}] > @{1}", ColumnName, safeName);
                        break;
                    case Comparison.GreaterThanOrEqual:
                        result = string.Format("[{0}] >= @{1}", ColumnName, safeName);
                        break;
                    case Comparison.LessThan:
                        result = string.Format("[{0}] < @{1}", ColumnName, safeName);
                        break;
                    case Comparison.LessThanOrEqual:
                        result = string.Format("[{0}] <= @{1}", ColumnName, safeName);
                        break;
                    case Comparison.NotEqual:
                        if (Value != System.DBNull.Value)
                        {
                            result = string.Format("[{0}] <> @{1}", ColumnName, safeName);
                        }
                        else
                        {
                            result = string.Format("[{0}] is not null", ColumnName, safeName);
                        }
                        break;
                    case Comparison.Like:
                        result = string.Format("[{0}] Like @{1}", ColumnName, safeName);
                        break;
                    case Comparison.Contains:
                        result = string.Format("Contains([{0}], @{1})", ColumnName, safeName);
                        break;
                    default:
                        if (Value != System.DBNull.Value)
                        {
                            result = string.Format("{0} = @{1}", ColumnName, safeName);
                        }
                        else
                        {
                            result = string.Format("{0} is null", ColumnName, safeName);
                        }
                        break;
                }
                return result;
            }
        }

        public void AddParam(string columnName, Comparison Comparison, object value)
        {
            Param newParam;


            newParam.ColumnName = columnName;

            // this ensures that all params have a unique name, which is useful if
            // the same param is used in more than one condition.
            newParam.ParameterName = String.Format("{0}{1}", columnName, Params.Count);

            // null values are implicitly treated as System.DBNull.Value
            if (value != null)
            {
                newParam.Value = value;
            }
            else
            {
                newParam.Value = System.DBNull.Value;
            }
            newParam.Comparison = Comparison;
            Params.Add(newParam);
        }

        public void AddParam(string columnName, object value)
        {
            AddParam(columnName, Comparison.Equal, value);
        }

        public struct OrderBy
        {
            public string Param;
            public Sort Direction;

            public override string ToString()
            {
                string result = "";
                switch (Direction)
                {
                    case Sort.Asc:
                        result = string.Format("[{0}] ASC", Param);
                        break;
                    default:
                        result = string.Format("[{0}] DESC", Param);
                        break;
                }
                return result;
            }
        }


        public void AddSort(string name, Sort Direction)
        {
            OrderBy newOrderBy;
            newOrderBy.Param = name;
            newOrderBy.Direction = Direction;
            OrderBys.Add(newOrderBy);
        }

        protected void AddSort(string name)
        {
            AddSort(name, Sort.Asc);
        }
    }
}